import javax.media.CaptureDeviceInfo;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
import javax.imageio.ImageIO;
import java.awt.Image;
import javax.media.util.BufferToImage;
import java.io.File;
import javax.media.CaptureDeviceManager;
import javax.media.control.FrameGrabbingControl;
import javax.media.format.VideoFormat;
import javax.media.Buffer;
import javax.media.Manager;
import javax.media.Player;

public class FrameGrab_Final {
	CaptureDeviceInfo deviceInfo;
	Player player;

	void cap(int num) {

		try {
			deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):1");
			player = Manager.createRealizedPlayer(deviceInfo.getLocator());
			player.start();
		} catch (Exception e) {
			System.out.println("Microsoft WDM  1 error");
			System.out.println(e);
			try {
				deviceInfo = CaptureDeviceManager.getDevice("vfw:Microsoft WDM Image Capture (Win32):0");
				player = Manager.createRealizedPlayer(deviceInfo.getLocator());
				player.start();
			} catch (Exception e2) {
				System.out.println("Microsoft WDM  0 error");
				System.out.println(e2);
				try {
					deviceInfo = CaptureDeviceManager.getDevice("vfw:Creative WebCam NX (VFW):0");
					player = Manager.createRealizedPlayer(deviceInfo.getLocator());
					player.start();
				} catch (Exception e1) {
					System.out.println("Creative 0 error");
					System.out.println(e1);
				}
			}
		}

		try {
			// Wait a few seconds for camera to initialise (otherwise img==null)
			Thread.sleep(2500);
			// Thread.sleep(100);

			// Grab a frame from the capture device
			FrameGrabbingControl frameGrabber = (FrameGrabbingControl) player.getControl("javax.media.control.FrameGrabbingControl");
			Buffer buf = frameGrabber.grabFrame();

			// Convert frame to an buffered image so it can be processed and
			// saved
			Image img = (new BufferToImage((VideoFormat) buf.getFormat()).createImage(buf));

			BufferedImage buffImg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);

			Graphics2D g = buffImg.createGraphics();
			g.drawImage(img, null, null);

			// Save image to disk as jpg
			// ---------------------------//Save img and not d:\\...input.jpgto
			// disk

			// ImageIO.write(buffImg, "png", new
			// File("e:\\gesture\\input"+num+".jpg"));

			// ImageIO.write(buffImg, "png", new
			// File("..\\gesture\\input"+num+".jpg"));
			ImageIO.write(buffImg, "png", new File("..\\input" + num + ".jpg"));

			/*
			 * resize1 a= new resize1(num);
			 * 
			 * 
			 * System.out.println("Resize1 over");
			 * 
			 * convert1 b= new convert1(num);
			 * 
			 * System.out.println("Convert1 over");
			 */

			Runtime r = Runtime.getRuntime();
			r.gc();

			player.close();
			player.deallocate();

			System.out.println("u r out ");
		} catch (Exception e2) {
			System.out.println("Exception " + e2);
			System.out.println(e2);
		}

	}

	public static void main(String args[]) {
		try {
			FrameGrab_Final f = new FrameGrab_Final();
			f.cap(1);
		} catch (Exception e) {
			System.out.println(e);
		}

	}

}
